package com.xuan.juc.volatiletest;

import java.util.concurrent.atomic.AtomicInteger;

/*
* 不保证原子性
* */
public class VTest2 {

//    private volatile static int num = 0;

//    原子类的 integer
    private volatile static AtomicInteger num = new AtomicInteger();

    public static void add(){
        num.getAndIncrement();
    }

    public static void main(String[] args) {
        for (int i = 0; i < 20; i++) {
            new Thread(()->{
                for (int j = 0; j < 500; j++) {
                    add();
                }
            }).start();
        }

        while (Thread.activeCount()>2){
            Thread.yield();
        }
        System.out.println(num);
    }

}
